let BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const webpack = require('webpack');
let path = require("path");
import fs from 'fs';
const packagejson = require("./package.json");

//版本号  规则是 version_number+1  如 0.0.1
const version_number = "0.1.";
function ganerServison() {
	const _path = `${__dirname}/public/version.js`;
	var str = fs.readFileSync(_path, "utf-8");
	let arr = str.split("=");
	let data = arr[1].replace(/\"/g, '')
	arr = data.split(version_number);
	if (data && arr.length > 1) {
		data = parseInt(arr[1]);
		if (data < 9) {
			data = "00" + (data + 1);
		} else if (data < 99) {
			data = "0" + (data + 1);
		} else {
			data = data + 1;
		}
		data = version_number + data;
	} else {
		data = version_number + "001";
	}
	data = 'var mi_version="' + data + '";//'+new Date().toLocaleString();
	fs.writeFileSync(_path, data);
}

module.exports = function (config, env) {
	if (env === 'production') {
		ganerServison();
		config.entry = {
			index: [ './src/index.js'],
			common: ['react', 'react-dom', 'dva']
		};
		//提取公共代码 antd common中的代码 路由按需加载时不会再次打包
		// config.plugins.push(
		// 	new webpack.optimize.CommonsChunkPlugin({ names: ["common"], minChunks: Infinity })
		// );

		// config.plugins.push(
		// 	new webpack.optimize.CommonsChunkPlugin({
		// 		async: 'antd-mobile',
		// 		minChunks(module) {
		// 			let context = module.context;
		// 			return context && context.indexOf('antd-mobile/lib') >= 0;
		// 		}
		// 	}));
		config.output = {
			path: path.resolve(__dirname, "dist"),
			// filename: "lib/[name].[chunkhash].js"
			filename: "lib/[name].js"
		};
		config.plugins.push(
			new webpack.DllReferencePlugin({
				context: __dirname,
				manifest: require('./public/dll/lib.manifest.json'),
			})
		)
		config.plugins.push(
			new webpack.DllReferencePlugin({
				context: __dirname,
				manifest: require('./public/dll/com.manifest.json'),
			})
		)
		config.plugins.push(
			new BundleAnalyzerPlugin({
				analyzerMode:"static"
			})
		)
		config.plugins.push(new webpack.optimize.AggressiveMergingPlugin());//合并块
		//压缩js
		config.plugins.push(new webpack.optimize.UglifyJsPlugin());
		//过滤掉moment中语言的本地化处理 只打包中文
		config.plugins.push(new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh\-cn/));
	}
	return config;
};